package com.cskaoyan.demo3.bean;

import lombok.Data;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/9 20:25
 */

@Data
public class PageInfo {
    private Integer page;
    private Integer limit;
    private String sort;
    private String order;

    private Integer offset;

    // 给这四个参数赋值，然后计算offset，并赋值
    public static PageInfo transfor(Map<String, String[]> parameterMap) {
        //new PageInfo对象
        PageInfo pageInfo = new PageInfo();
        // 从parameterMap中获取page, limit, sort, order的值
        // 并设置到PageInfo对象中
        try {
            BeanUtils.copyProperties(pageInfo, parameterMap);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        // 计算offset的值
        Integer offset = (pageInfo.getPage() - 1) * pageInfo.getLimit();
        // 并设置到PageInfo对象中
        pageInfo.setOffset(offset);
        // 返回PageInfo对象
        return pageInfo;
    }


}
